//**************************************************************
//		emptySourceView.m
//			AppHider
//
//	Copyright (c) 2008 Metta Media
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//**************************************************************
#import "emptySourceView.h"


@implementation emptySourceView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // theAnim is an NSAnimation instance variable.
//        theAnim = [[NSAnimation alloc] initWithDuration:11.0
//										 animationCurve:NSAnimationEaseInOut];
//        [theAnim setFrameRate:20.0];
//        [theAnim setAnimationBlockingMode:NSAnimationNonblocking];
//		[theAnim startWhenAnimation:theAnim reachesProgress:1.0];
//        [theAnim setDelegate:self];
		NSBeep();
    }
    return self;
}

-(void)awakeFromNib
{
	[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,nil]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
	NSBeep();
    NSPasteboard *pboard = [sender draggingPasteboard];
	if([sender draggingSource] == nil)
	{
		if ([[pboard types] containsObject:NSFilenamesPboardType])
		{
			NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
			int i;
			int error = 0;
			for (i=0;i<[filenames count];i++)
			{
				if([[[filenames objectAtIndex:i] pathExtension] isNotEqualTo:@"app"])
				{
					error++;
				}
			}
			if(error>0)
			{ 
				return NSDragOperationNone;
			}
			else
			{
				return NSDragOperationGeneric;
			}
		}
	}
	return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
    //we aren't particularily interested in this so we will do nothing
    //this is one of the methods that we do not have to implement
}

// Work around a bug from 10.2 onwards
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
	return NSDragOperationEvery;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
	NSPasteboard *pboard = [sender draggingPasteboard];
	if([sender draggingSource] == nil)
	{
		if ([[pboard types] containsObject:NSFilenamesPboardType])
		{
			NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
			int i;
			int error = 0;
			for (i=0;i<[filenames count];i++)
			{
				if([[[filenames objectAtIndex:i] pathExtension] isNotEqualTo:@"app"])
				{
					error++;
				}
			}
			if(error>0)
			{ 
				return NSDragOperationNone;
			}
			else
			{
				return NSDragOperationGeneric;
			}
		}
	}
	return NSDragOperationNone;
}

- (void)draggingEnded:(id <NSDraggingInfo>)sender
{
    //we don't do anything in our implementation
    //this could be ommitted since NSDraggingDestination is an infomal
	//protocol and returns nothing
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];
	
	if([sender draggingSource] == nil)
	{
		if ([[pboard types] containsObject:NSFilenamesPboardType])
		{
			NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
			int i;
			for (i=0;i<[filenames count];i++)
			{
				[datasource performSelector:@selector(addAppFromDragNDrop:) withObject:[filenames objectAtIndex:i]];
			}
			return YES;
		}
		else
		{
			return NO;
		}
	}
    return NO;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    //re-draw the view with our new data
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect {
	[[NSColor colorWithDeviceRed:221.0/255.0 green:228.0/255.0 blue:234.0/255.0 alpha:255.0/255.0] setFill];
	NSRectFill(rect);
	
	
	int borderPadding = 10;
	NSRect dropRect = NSMakeRect(borderPadding, 40+borderPadding, rect.size.width-6-borderPadding, rect.size.height-48-borderPadding);
	[[NSColor grayColor] setStroke];
	NSBezierPath* bezierDropRect = [NSBezierPath bezierPathWithRoundedRect:dropRect xRadius:10.0 yRadius:10.0];
	[bezierDropRect setLineWidth:2.5];
	float dash[2] = { 10.0, 5.0 };
	[bezierDropRect setLineDash:dash count:2 phase:0.0];
	[bezierDropRect stroke];
	
	
	// Draw arrow pointing at + button
	NSPoint arrowPoint = NSMakePoint(2.0, 10.0);
    CGFloat scaleFactor = [[self window] userSpaceScaleFactor];
    if (scaleFactor != 1.0)
	{
		NSPoint    tempPoint = [self convertPointToBase:arrowPoint];
		tempPoint.x = floor(tempPoint.x);
		tempPoint.y = floor(tempPoint.y);
		arrowPoint = [self convertPointFromBase:tempPoint];
	}
	NSBezierPath* arrowPath = [NSBezierPath bezierPath];
	[arrowPath setLineWidth:2.0];
	[arrowPath setLineJoinStyle:NSRoundLineJoinStyle];
	[[NSColor whiteColor] setStroke];
	[arrowPath moveToPoint:arrowPoint];
	[arrowPath lineToPoint:NSMakePoint(12.0, 1.0)];
	[arrowPath lineToPoint:NSMakePoint(22.0, 10.0)];
	[arrowPath lineToPoint:NSMakePoint(17.0, 10.0)];
	[arrowPath lineToPoint:NSMakePoint(17.0, 20.0)];
	[arrowPath lineToPoint:NSMakePoint(7.0, 20.0)];
	[arrowPath lineToPoint:NSMakePoint(7.0, 10.0)];
	[arrowPath closePath];
	NSGradient* arrowGrad = [[NSGradient alloc] initWithStartingColor:[NSColor grayColor] endingColor:[NSColor darkGrayColor]];
	[arrowGrad drawInBezierPath:arrowPath angle:-90.0];
	[arrowPath stroke];
	
	// Draw 'Add App' Text
	NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init];
	[attributes setObject:[NSFont labelFontOfSize:16.0] forKey:NSFontAttributeName];
	[attributes setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
	NSPoint textPoint = NSMakePoint(25.0, 1.0);
	if (scaleFactor != 1.0)
	{
		NSPoint    tempPoint = [self convertPointToBase:textPoint];
		tempPoint.x = floor(tempPoint.x);
		tempPoint.y = floor(tempPoint.y);
		textPoint = [self convertPointFromBase:tempPoint];
	}
	[@"Click to add\nan appication" drawAtPoint:textPoint withAttributes:attributes];
	 [super drawRect:rect];
}

@end
